package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;

import core.EventBus;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class StartBox extends JPanel implements MouseListener{
	private Color c = null;
	private String colorName = "";
	private final int rectSide = 45;
	private int pieces = 0;
	private int room = 4; //Quick hack to reverse the order the pieces are popped in.
	private Piece[] ps = new Piece[4];
	private EventBus m; //PROBLEM!
	private int offsetX = 10;
	private int offsetY = 10;

	public StartBox(Color c, String colorName, EventBus m) {
		this.m = m;
		this.setLayout(null);
		this.setSize(150, 120);
		this.setVisible(true);
		this.c = c;
		this.addMouseListener(this);
		this.colorName = colorName;
		repaint();

	}

	public void addPiece(Piece comp){
		if(pieces < 4){
			Component c = super.add(comp);
			ps[pieces] = comp;
			pieces ++;
			room --;

			switch(pieces){
			case 1: c.setBounds(25+offsetX, 10+offsetY, 50, 50); break;
			case 2: c.setBounds(80+offsetX, 10+offsetY, 50, 50); break;
			case 3: c.setBounds(25+offsetX, 65+offsetY, 50, 50); break;
			case 4: c.setBounds(80+offsetX, 65+offsetY, 50, 50); break;
			}
		}
	}

	public Piece popPiece(){
		if(pieces > 0){
			System.out.println(room);
			this.remove(ps[room]);
			Piece p = ps[room];
			p.setBounds(10, 10, 25, 25);
			ps[room] = null;
			pieces --;
			room ++;
			System.out.println(room);
			return p;
		}
		return null;
	}

	@Override
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;		
		g2.setColor(c);
		g2.fillRect(25, 10, rectSide, rectSide);
		g2.fillRect(80, 10, rectSide, rectSide);
		g2.fillRect(25, 65, rectSide, rectSide);
		g2.fillRect(80, 65, rectSide, rectSide);
		this.paintChildren(g);
	}	
	@Override
	public void mouseClicked(MouseEvent a) {
		int x = a.getX();
		int y = a.getY();
		
		if(x > 25 && x < 70 && y > 10 && y < 55){
			System.out.println(x+","+y+": Square TopLeft. Team: "+ colorName); 
			if(ps[0] != null) m.handleEvent(new ActionEvent(ps[0], 0, "MOVE"));
		}
		else if(x > 80 && x < 125 && y > 10 && y < 55){
			System.out.println(x+","+y+": Square TopRight Team: "+ colorName);
			if(ps[1] != null) m.handleEvent(new ActionEvent(ps[1], 0, "MOVE"));
		}
		else if(x > 25 && x < 70 && y > 65 && y < 110){
			System.out.println(x+","+y+": Square BL Team: "+ colorName);
			if(ps[2] != null) m.handleEvent(new ActionEvent(ps[2], 0, "MOVE"));
		}
		else if(x > 80 && x < 125 && y > 65 && y < 110){
			System.out.println(x+","+y+": Square BR Team: "+ colorName);
			if(ps[3] != null) m.handleEvent(new ActionEvent(ps[3], 0, "MOVE"));
		}


	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
}
